package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<CourseTeacher> getCourseTeacher(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeacher = courseTeacherMapper.selectList(queryWrapper);

        return courseTeacher;
    }

    @Override
    public CourseTeacher saveCourseTeacher(CourseTeacherDto courseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher() ;
        if(StringUtils.isEmpty(courseTeacherDto.getId())){
            BeanUtils.copyProperties(courseTeacherDto,courseTeacher);

            String introduction = courseTeacherDto.getIntroduction();
            courseTeacher.setCreateDate(LocalDateTime.now());
            if(StringUtils.isEmpty(introduction)){
                courseTeacher.setIntroduction("这个家伙很懒，什么都没写");
            }
            int insert = courseTeacherMapper.insert(courseTeacher);
            if(insert<=0 ){
                throw new RuntimeException("添加教师失败");
            }
        }else{
            BeanUtils.copyProperties(courseTeacherDto,courseTeacher);

            String introduction = courseTeacherDto.getIntroduction();
//            courseTeacher.setCreateDate(LocalDateTime.now());
            if(StringUtils.isEmpty(introduction)){
                courseTeacher.setIntroduction("这个家伙很懒，什么都没写");
            }
            int insert = courseTeacherMapper.updateById(courseTeacher);
            if(insert<=0 ){
                throw new RuntimeException("修改教师失败");
            }
        }

        return courseTeacher ;
    }

    @Override
    public List<CourseTeacher> selectByCompanyId(Long companyId) {
        return    courseTeacherMapper.selectByCompanyId(companyId);
    }

    @Override
    public List<CourseTeacher> selectByCompanyIdAndCourseId(@Param("courseId")Long courseId, @Param("companyId")Long companyId) {
        return courseTeacherMapper.selectCourseTeacher(courseId,companyId);
    }

    @Override
    public void deleteCourseTeacher(Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId).eq(CourseTeacher::getId,id);
        int i = courseTeacherMapper.delete(queryWrapper);
        if(i<=0){
            throw new RuntimeException("删除教师失败");
        }
    }
}